package Repositorio;

import View.Novo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RepositorioEquipamentos {

    private Connection conn;
    private String query;
    private PreparedStatement stmt;
    private ResultSet rs;

    public void getAllEquipaments() throws SQLException {
        conn = LinkDB.Conexao.getConexao();
        query = "SELECT nameEquipamento FROM equipamento ORDER BY idEquipamento ASC";
        stmt = conn.prepareStatement(query);
        rs = stmt.executeQuery();
        while(rs.next()){
            Novo.equipamento.addItem(rs.getString("nameEquipamento"));
        }
        rs.close();
        stmt.close();
        conn.close();        
    }
    
    public void insertEquipamento(String nameEquipamento_ref) throws SQLException{
        conn = LinkDB.Conexao.getConexao();
        query = "INSERT INTO equipamento(nameEquipamento) VALUES(?)";
        stmt = conn.prepareStatement(query);
        stmt.setString(1, nameEquipamento_ref);
        stmt.executeUpdate();
        stmt.close();
        conn.close();   
    }
    
    public int idEquipamento(String nameEquipamento) throws SQLException{
        conn = LinkDB.Conexao.getConexao();
        query = "SELECT idEquipamento FROM equipamento WHERE nameEquipamento = ?";
        stmt = conn.prepareStatement(query);
        stmt.setString(1, nameEquipamento);
        rs = stmt.executeQuery();
        if (rs.next()) {
            return Integer.parseInt(rs.getString("idEquipamento"));
        } else {
            return 0;
        }
    }
}
